Skip to content

Use default in TypeVar so Series defaults to Series[Any], and Index to Index[Any] #1232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 17, 2025

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented May 29, 2025

bit of an experiment

closes #1133

I saw that NumPy follow this pattern, for example here:

https://github.com/numpy/numpy/blob/3363b38d99f87601869c2c9d68c2b16e54509675/numpy/__init__.pyi#L788-L792

and they import TypeVar from typing_extensions (which is a dependency of mypy anyway)

This would close 1133 for free, and I think improve ergonomics

@MarcoGorelli MarcoGorelli force-pushed the typevar-default branch 3 times, most recently from 1a6dbd7 to f63956a Compare May 29, 2025 10:27
@MarcoGorelli MarcoGorelli changed the title Use default in TypeVar so Series defaults to Series[Any], and Index to Index[Any] WIP: Use default in TypeVar so Series defaults to Series[Any], and Index to Index[Any] May 29, 2025
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 29, 2025

I'm fine with this. Need to run black. The new default for TypeVar is in python 3.13, so that's why you have to import TypeVar from typing_extensions

@MarcoGorelli
Copy link
Member Author

cool, thanks! there's a couple of test failures I haven't figured out, will investigate

tests/test_scalars.py:1382: error: Expression is of type "Any", not "ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]]"  [assert-type]
tests/test_scalars.py:1385: error: Expression is of type "Any", not "ndarray[tuple[int, ...], dtype[numpy.bool[builtins.bool]]]"  [assert-type]

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK so far.

@@ -168,7 +168,7 @@ class SeriesGroupBy(GroupBy[Series[S1]], Generic[S1, ByT]):
self,
) -> Iterator[tuple[ByT, Series[S1]]]: ...

_TT = TypeVar("_TT", bound=Literal[True, False])
_TT = TypeVar("_TT", bound=Literal[True, False], default=Literal[True])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised that you didn't have to change the import of TypeVar to come from typing_extensions here since you are using the default feature.

@MarcoGorelli
Copy link
Member Author

i think there might be an issue with mypy here, have reported python/mypy#19182

),
pd.DataFrame,
)
check(
assert_type(
df_multi_level_cols2.stack([0, 1]),
Union[pd.DataFrame, "pd.Series[Any]"],
Union[pd.DataFrame, "pd.Series"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any of the places where we used to have "pd.Series[Any]" that you changed to "pd.Series" can have the quotes removed. The quotes were there because of the [Any] part.

@MarcoGorelli MarcoGorelli changed the title WIP: Use default in TypeVar so Series defaults to Series[Any], and Index to Index[Any] Use default in TypeVar so Series defaults to Series[Any], and Index to Index[Any] Jun 10, 2025
@MarcoGorelli MarcoGorelli marked this pull request as ready for review June 10, 2025 08:50
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @MarcoGorelli . Nice new feature of typing to include.

@Dr-Irv Dr-Irv merged commit 709d745 into pandas-dev:main Jun 17, 2025
13 checks passed
@MarcoGorelli
Copy link
Member Author

yay, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should Series[Any] be used internally instead of Series?
2 participants